/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package CapaSecretaria;

import Capamixta.MainMenu;
import capanegocio.itinerario;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author Seralan
 */
public class itinerarios extends javax.swing.JFrame {

    /**
     * Creates new form itinerarios_buses
     */
    int contador=0;
    public itinerarios() {
        initComponents();
        setResizable(false);//metodo que no permite la maximizacion
        setLocationRelativeTo(null);//cuando se abra la ventana se abre en el centro de la pantalla
        setTitle("Registro Itinerario");//para darle nombre a la ventana
        setSize(800, 600);//Tamaño de la ventana
        buses.removeAll();
        Combobox();
            }
    public void Combobox(){
        
        try{
        Connection c=DriverManager.getConnection("jdbc:mysql://localhost/agua_azul","root","");
        Statement sent=c.createStatement();
        ResultSet rs= sent.executeQuery("Select Pantente from bus");
        while (rs.next()){
            this.buses.addItem(rs.getString("Pantente"));
        }
        contador++;
    }catch (Exception e){
            JOptionPane.showMessageDialog(null, e);
            }
        
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbuttonactualizar_i = new javax.swing.JButton();
        jtable_itinerario = new javax.swing.JScrollPane();
        itinerarios = new javax.swing.JTable();
        jtextcodigo = new javax.swing.JLabel();
        buses = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        cod = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        Salir = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jbuttonactualizar_i.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jbuttonactualizar_i.setText("Buscar itinerarios");
        jbuttonactualizar_i.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbuttonactualizar_iActionPerformed(evt);
            }
        });

        itinerarios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Codigo Traslado ", "Bus", "Rut Pasajero", "Hora Salida", "Hora Llegada"
            }
        ));
        itinerarios.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                itinerariosMouseClicked(evt);
            }
        });
        jtable_itinerario.setViewportView(itinerarios);

        jtextcodigo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jtextcodigo.setText("Patente Bus");

        buses.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        jButton1.setText("Borrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Codigo Traslado: ");

        jMenu1.setText("Menu");

        jMenuItem4.setText("Colegio");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuItem3.setText("Clientes");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuItem2.setText("Registro Pagos");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        Salir.setText("Salir");
        Salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SalirActionPerformed(evt);
            }
        });
        jMenu1.add(Salir);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jtable_itinerario, javax.swing.GroupLayout.PREFERRED_SIZE, 724, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtextcodigo)
                            .addComponent(jbuttonactualizar_i))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(buses, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cod, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(192, 192, 192)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtextcodigo)
                    .addComponent(buses, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cod, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jbuttonactualizar_i)
                        .addComponent(jButton1)
                        .addComponent(jLabel2)))
                .addGap(31, 31, 31)
                .addComponent(jtable_itinerario, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbuttonactualizar_iActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbuttonactualizar_iActionPerformed
            updateTabla();
    }//GEN-LAST:event_jbuttonactualizar_iActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
         colegios obj=new colegios(); //creacion de nuevo objeto o frame
        obj.setVisible(true);// visible nuevo frame (colegios ej)
        dispose();//cierra ventana actual 
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
         cliente obj=new cliente(); //creacion de nuevo objeto o frame
        obj.setVisible(true);// visible nuevo frame (colegios ej)
        dispose();//cierra ventana actual 
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
         Pagos obj=new Pagos(); //creacion de nuevo objeto o frame
        obj.setVisible(true);// visible nuevo frame (colegios ej)
        dispose();//cierra ventana actual 
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void itinerariosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_itinerariosMouseClicked
    fila = itinerarios.rowAtPoint(evt.getPoint());                 
         if (fila > -1){                          
             cod.setText(String.valueOf(itinerarios.getValueAt(fila, 0)));
                               }
    }//GEN-LAST:event_itinerariosMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        p.deleteItinerario(Integer.parseInt(cod.getText()));
        updateTabla();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void SalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SalirActionPerformed
        MainMenu obj=new MainMenu(); //creacion de nuevo objeto o frame
        obj.setVisible(true);// visible nuevo frame (colegios ej)
        dispose();//cierra ventana actual        // TODO add your handling code here:
    }//GEN-LAST:event_SalirActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(itinerarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(itinerarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(itinerarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(itinerarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new itinerarios().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem Salir;
    private javax.swing.JComboBox buses;
    private javax.swing.JLabel cod;
    private javax.swing.JTable itinerarios;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JButton jbuttonactualizar_i;
    private javax.swing.JScrollPane jtable_itinerario;
    private javax.swing.JLabel jtextcodigo;
    // End of variables declaration//GEN-END:variables
Object [][] dtPer;
    int fila=-1;
     itinerario p=new itinerario();
    //mostrar datos de colegios desde la base de datos
    private void updateTabla(){
        Object e = buses.getSelectedItem(); 
        String patente=String.valueOf(e);
        String [] ColumnNames={"Cod Traslado","Patente Bus", "Rut Pasajero","Hora Salida","Hora Llegada" };
        dtPer=p.getDatos(patente);
        DefaultTableModel Datos=new DefaultTableModel(dtPer,ColumnNames);
        itinerarios.setModel(Datos);
        TableColumn columna=itinerarios.getColumn("Cod Traslado");
        }
}
